package com.ddtou;


import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import net.sf.json.JSONObject;

public class DbUtil {
	private static Connection conn = null;  
	private static Statement statement = null;  
	
	public DbUtil(){
		if(conn==null){
			this.getConection();
		}
		
	}

	
    public void getConection() {
        System.out.println("加载数据库配置文件");
        Properties prop = new Properties();
        InputStream fins = null;
        String strUrl;
        String username;
        String password;
        try {

            fins = DbUtil.class.getResourceAsStream("/com/ddtou/init.properties");
            prop.load(fins);
            prop.list(System.out);
        } catch (IOException e) {
            System.out.print("加载配置文件失败");
            e.printStackTrace();
        }
        strUrl = prop.getProperty("strUrl");
        username = prop.getProperty("username");
        password = prop.getProperty("password");
        System.out.println(strUrl);
        try {
            Class.forName("org.postgresql.Driver").newInstance();
            conn = DriverManager.getConnection(strUrl, username, password);
        } catch (Exception e) {
            System.out.println("数据库连接失败，请检查配置");
            e.printStackTrace();
        }

    }
	/**
	 * @param args
	 */

	public static void main(String[] args) throws SQLException {

	  
	    // connect to MySQL  
		JSONObject json = new JSONObject();
		DbUtil  m = new DbUtil();
		m.getConection();
}

    private void connSQL() {  
		String urlpg = "jdbc:postgresql://localhost:5432/mymoon";  
        String username = "postgres";  
        String password = "q"; // 加载驱动程序以连接数据库   
        try {   
            Class.forName("org.postgresql.Driver").newInstance();   
            conn = DriverManager.getConnection( urlpg,username, password ); 
        }
        //捕获加载驱动程序异常  
         catch (Exception cnfex ) {  
             System.out.println("装载 JDBC/ODBC 驱动程序失败。" );  
         }
    }
    
    public void PostgreSQLJDBC() { 
      try { 
         Class.forName("org.postgresql.Driver"); 
         conn = DriverManager 
            .getConnection("jdbc:postgresql://localhost:5432/moondata", 
            "postgres", "q"); 
      } catch (Exception e) { 
         e.printStackTrace(); 
         System.err.println(e.getClass().getName()+": "+e.getMessage()); 
         System.exit(0); 
      } 
      System.out.println("Opened database successfully"); 
   } 

    
    public  void  TestDB()  {

    	String urlpg = "jdbc:postgresql://localhost:5432/moondata";
		String urlmysql = "jdbc:mysql://localhost:3307/moondata?characterEncoding=UTF-8";
        String username = "postgres"; 
        String usermysql = "root";  
        String password = "q"; // 加载驱动程序以连接数据库   
         try 
          {
            Class.forName( "com.mysql.jdbc.Driver").newInstance();
            String url = "jdbc:postgresql://localhost:5432/moondata" ;
            Connection con = DriverManager.getConnection(urlmysql, "root" , "q" );
            Statement st = con.createStatement();
            String sql = " select * from data" ;
            ResultSet rs = st.executeQuery(sql);
             while (rs.next())
             {
                System.out.print(rs.getString(1));
                //System.out.println(rs.getInt(2));
            } 
            rs.close();
            st.close();
            con.close();
            

        } 
         catch (Exception ee)
         {
            System.out.print(ee.getMessage());
        } 
    }
    
    
    // disconnect to MySQL  
    public void deconnSQL() {  
        try {  
            if (conn != null){
            	conn.close(); 
            	conn = null;
            }  
                
        } catch (Exception e) {  
            System.out.println("关闭数据库问题 ：");  
            e.printStackTrace();  
        }  
    }  
  
    // execute selection language  
    public ResultSet selectSQL(String sql) {  
        ResultSet rs = null;  
        try {  
            statement = conn.createStatement();  
            rs = statement.executeQuery(sql);  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
        return rs;  
    }  
  
    // execute insertion language  
    public boolean insertSQL(String sql) {  
    	boolean b = true;
        try {  
            statement = conn.createStatement();
            statement.executeUpdate(sql);  
            
        } catch (SQLException e) {  
        	b=false;
            System.out.println("插入数据库时出错：");  
            e.printStackTrace(); 
        } catch (Exception e) {  
        	b=false;
            System.out.println("插入时出错：");  
            e.printStackTrace();  
        }  
        return b;  
    }  
    //execute delete language  
    public boolean deleteSQL(String sql) {  
        try {  
        	statement = conn.createStatement();
            statement.executeUpdate(sql); 
            return true;  
        } catch (SQLException e) {  
            System.out.println("插入数据库时出错：");  
            e.printStackTrace();  
        } catch (Exception e) {  
            System.out.println("插入时出错：");  
            e.printStackTrace();  
        }  
        return false;  
    }  
    //execute update language  
    public boolean updateSQL(String sql) {  
        try {  
        	statement = conn.createStatement();
            statement.executeUpdate(sql);
            return true;  
        } catch (SQLException e) {  
            System.out.println("插入数据库时出错：");  
            e.printStackTrace();  
        } catch (Exception e) {  
            System.out.println("插入时出错：");  
            e.printStackTrace();  
        }  
        return false;  
    }  
}